package com.hsomandroidapp.list;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

public class ProductListAdapter extends BaseAdapter {

	private List<ListItem> resultList;
	private Context context;
	private LayoutInflater layoutInflater;

	public ProductListAdapter(Context context) {
		this.context = context;
		this.resultList = new ArrayList<ListItem>();
		this.layoutInflater = (LayoutInflater) context
				.getSystemService("layout_inflater");
	}

	@Override
	public int getCount() {
		return this.resultList.size();
	}

	@Override
	public Object getItem(int arg0) {
		return this.resultList.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		return this.resultList.get(position).getViewForListElement(
				layoutInflater, context, convertView);
	}

	public void addList(ArrayList<ListItem> elements) {
		Iterator<ListItem> it = elements.iterator();
		ListItem item;
		while (it.hasNext()) {
			item = it.next();
			if (!this.resultList.contains(item)) {
				addElement(item);
			}
		}

	}

	public void addElement(ListItem element) {

		this.resultList.add(element);
	}

}
